package com.freeland.client2009.webservices.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.freeland.client2009.dao.ibatis.model.AdState;
import com.freeland.client2009.dao.ibatis.model.ComputerMac;
import com.freeland.client2009.dao.ibatis.model.ComputerPowerInfo;
import com.freeland.client2009.dao.ibatis.model.NetBarInfo;
import com.freeland.client2009.service.HotelComputerService;
import com.freeland.client2009.webservices.HotelService;


@WebService(endpointInterface="com.freeland.client2009.webservices.HotelService")
@Component("hotelServiceImpl")
public class HotelServiceImpl implements HotelService {
	
	private final int PAGE_SIZE=10;

	@Autowired
	private HotelComputerService hotelComputerService;
	
	
	public String getAdstatePage(int pageIndex, int pageSize, String userId,
			String userType) {
		String sRet ="-9999";
		Long totalSize =hotelComputerService.getAdStateCount(userType, userId,null);
		if(totalSize>0){
			if(pageSize==0)
				pageSize =PAGE_SIZE;
			if(pageIndex<0)
				pageIndex=0;
			
			double totalResults = totalSize.doubleValue();
			int allPage =new Double(totalResults /pageSize).intValue();
			if(totalResults %pageSize >0)
				allPage +=1;
			
			if(allPage>=pageIndex){
				List<AdState> list =hotelComputerService.getAdStatePage(pageIndex, pageSize, userType, userId,null);
				sRet =buildAdStateXml(pageIndex,pageSize,allPage,totalSize,list);
			}else{
				//sRet ="-1";//没有数据
				sRet =buildAdStateXml(pageIndex,pageSize,allPage,totalSize,new ArrayList<AdState>());
			}
		}else{
			//sRet ="-1";//没有数据
			sRet =buildAdStateXml(pageIndex,pageSize,0,0L,new ArrayList<AdState>());
		}
		return sRet;
	}
	private String buildAdStateXml(int pageIndex,int pageSize,int allPage,Long totalSize,List<AdState> adStateList) {
		String sRet;
		sRet ="<table><page><pageindex>{0}</pageindex><pagesize>{1}</pagesize><allpage>{2}</allpage><currentsize>{3}</currentsize><totalsize>{4}</totalsize></page><adstates>{5}</adstates></table>";
		StringBuilder netbarInfoRes = new StringBuilder();
		String netbarinfoTemplate = "<adstate><useracct>{0}</useracct><username>{1}</username><usertype>{2}</usertype><gameid>{3}</gameid><gamename>{4}</gamename><display>{5}</display></adstate>";
		
		for(AdState adState : adStateList){
			netbarInfoRes.append(MessageFormat.format(netbarinfoTemplate,new Object[]{adState.getUseracct(),adState.getUsername(),adState.getUsertype(),adState.getGameid(),adState.getGamename()==null?"":adState.getGamename(),adState.getDisplay()}));
		}
		sRet = MessageFormat.format(sRet,String.valueOf(pageIndex),String.valueOf(pageSize),String.valueOf(allPage),String.valueOf(adStateList.size()),totalSize!=null?totalSize.toString():0,netbarInfoRes.toString());
		return sRet;
	}
	
	public String getComputerUseLogPageWithMinute(int pageIndex, int pageSize,	String userId, String userType, String startDate, String endDate,int minute) {
		String sRet ="-9999";
		Long totalSize =hotelComputerService.getComputerUseLogCount(userType, userId,null, startDate, endDate,minute);
		if(totalSize>0){
			if(pageSize==0)
				pageSize =PAGE_SIZE;
			if(pageIndex<0)
				pageIndex=0;
			
			double totalResults = totalSize.doubleValue();
			int allPage =new Double(totalResults /pageSize).intValue();
			if(totalResults %pageSize >0)
				allPage +=1;
			
			if(allPage>=pageIndex){
				List<ComputerPowerInfo>  list = hotelComputerService.getComputerUseLogPage(pageIndex, pageSize, userType, userId,null, startDate, endDate,minute);
			    sRet =buildComputerPowerInfoXml(pageIndex,pageSize,allPage,totalSize,list);
			}else{
				//sRet ="-1";//没有数据
				sRet =buildComputerPowerInfoXml(pageIndex,pageSize,allPage,totalSize,new ArrayList<ComputerPowerInfo>());
			}
		}else{
			//sRet ="-1";//没有数据
			sRet =buildComputerPowerInfoXml(pageIndex,pageSize,0,0L,new ArrayList<ComputerPowerInfo>());
		}
		return sRet;
	}
	public String getComputerUseLogPage(int pageIndex, int pageSize,
			String userId, String userType, String startDate, String endDate) {
		return this.getComputerUseLogPageWithMinute(pageIndex, pageSize, userId, userType, startDate, endDate, 0);
	}
	private String buildComputerPowerInfoXml(int pageIndex,int pageSize,int allPage,Long totalSize,List<ComputerPowerInfo> computerPowerInfoList) {
		String sRet;
		sRet ="<table><page><pageindex>{0}</pageindex><pagesize>{1}</pagesize><allpage>{2}</allpage><currentsize>{3}</currentsize><totalsize>{4}</totalsize></page><computerpowerinfos>{5}</computerpowerinfos></table>";
		StringBuilder netbarInfoRes = new StringBuilder();
		String netbarinfoTemplate = "<computerpowerinfo><useracct>{0}</useracct><username>{1}</username><usertype>{2}</usertype><computermac>{3}</computermac><computername>{4}</computername><powertime>{5}</powertime><offtime>{6}</offtime></computerpowerinfo>";
		
		for(ComputerPowerInfo computerPowerInfo : computerPowerInfoList){
			netbarInfoRes.append(MessageFormat.format(netbarinfoTemplate,new Object[]{computerPowerInfo.getUseracct(),computerPowerInfo.getUsername(),computerPowerInfo.getUsertype(),computerPowerInfo.getComputermac(),computerPowerInfo.getComputername(),computerPowerInfo.getOfftime(),computerPowerInfo.getPowertime()}));
		}
		sRet = MessageFormat.format(sRet, String.valueOf(pageIndex),String.valueOf(pageSize),String.valueOf(allPage),String.valueOf(computerPowerInfoList.size()),totalSize!=null?totalSize.toString():0,netbarInfoRes.toString());
		return sRet;
	}

	public String getNetBarPCMacPage(int pageIndex, int pageSize, String userId,
			String userType) {
		String sRet = "-9999" ;
		Long totalSize =hotelComputerService.getComputerMacCount(userType, userId,null);
		if(totalSize>0){
			if(pageSize==0)
				pageSize =PAGE_SIZE;
			if(pageIndex<0)
				pageIndex=0;
			
			double totalResults = totalSize.doubleValue();
			int allPage =new Double(totalResults /pageSize).intValue();
			if(totalResults %pageSize >0)
				allPage +=1;
			
			if(allPage>=pageIndex){
				List<ComputerMac> list =hotelComputerService.getComputerMacPage(pageIndex, pageSize, userType, userId,null);

				sRet =buildNetBarPCMacXml(pageIndex,pageSize,allPage,totalSize,list);
				
			}else{
				//sRet="-1";//页号超出没有数据
				sRet =buildNetBarPCMacXml(pageIndex,pageSize,allPage,totalSize,new ArrayList<ComputerMac>());
			}
		}else{
			//sRet ="-1";//没有数据
			sRet =buildNetBarPCMacXml(pageIndex,pageSize,0,0L,new ArrayList<ComputerMac>());
		}
		return sRet;
	}
	private String buildNetBarPCMacXml(int pageIndex,int pageSize,int allPage,Long totalSize,List<ComputerMac> netBarPcMacList) {
		String sRet;
		sRet ="<table><page><pageindex>{0}</pageindex><pagesize>{1}</pagesize><allpage>{2}</allpage><currentsize>{3}</currentsize><totalsize>{4}</totalsize></page><netbarpcmacs>{5}</netbarpcmacs></table>";
		StringBuilder netbarInfoRes = new StringBuilder();
		String netbarinfoTemplate = "<netbarpcmac><useracct>{0}</useracct><username>{1}</username><usertype>{2}</usertype><mac>{3}</mac></netbarpcmac>";
		
		for(ComputerMac netBarPcmac : netBarPcMacList){
			netbarInfoRes.append(MessageFormat.format(netbarinfoTemplate,new Object[]{netBarPcmac.getUseracct(),netBarPcmac.getUsername(),netBarPcmac.getUsertype(),netBarPcmac.getMac()}));
		}
		sRet = MessageFormat.format(sRet,String.valueOf(pageIndex),String.valueOf(pageSize),String.valueOf(allPage),String.valueOf(netBarPcMacList.size()),totalSize!=null?totalSize.toString():0,netbarInfoRes.toString());
		return sRet;
	}
	public String getNetBarInfoPage(int pageIndex, int pageSize,String userId,String userType) {
		String sRet = "-9999" ;
		
		Long totalSize =hotelComputerService.getNetBarInfoCount( userType, userId,null);
		if(totalSize>0){
			if(pageSize==0)
				pageSize =PAGE_SIZE;
			if(pageIndex<0)
				pageIndex=0;
			
			double totalResults = totalSize.doubleValue();
			int allPage =new Double(totalResults /pageSize).intValue();
			if(totalResults %pageSize >0)
				allPage +=1;
			
			if(allPage>=pageIndex){
				List<NetBarInfo> list = hotelComputerService.getNetBarInfoPage(pageIndex, pageSize, userType, userId,null);
				
				sRet =buildNetBarInfoXml(pageIndex,pageSize,allPage,totalSize,list);
				
			}else{
				//sRet="-1";//页号超出没有数据
				sRet =buildNetBarInfoXml(pageIndex,pageSize,allPage,totalSize,new ArrayList<NetBarInfo>());
			}
		}else{
			//sRet ="-1";//没有数据
			sRet =buildNetBarInfoXml(pageIndex,pageSize,0,0L,new ArrayList<NetBarInfo>());
		}
		return sRet;
	}
	private String buildNetBarInfoXml(int pageIndex,int pageSize,int allPage,Long totalSize,List<NetBarInfo> netBarInfoList) {
		String sRet;
		sRet ="<table><page><pageindex>{0}</pageindex><pagesize>{1}</pagesize><allpage>{2}</allpage><currentsize>{3}</currentsize><totalsize>{4}</totalsize></page><netbarinfos>{5}</netbarinfos></table>";
		StringBuilder netbarInfoRes = new StringBuilder();
		String netbarinfoTemplate = "<netbarinfo><useracct>{0}</useracct><username>{1}</username><onlinestate>{2}</onlinestate><powertime>{3}</powertime><usertype>{4}</usertype></netbarinfo>";
		
		for(NetBarInfo netBarInfo : netBarInfoList){
			netbarInfoRes.append(MessageFormat.format(netbarinfoTemplate,new Object[]{netBarInfo.getUseracct(),netBarInfo.getUsername(),netBarInfo.getOnlinestate(),netBarInfo.getPowertime(),netBarInfo.getUsertype()}));
		}
		sRet = MessageFormat.format(sRet,String.valueOf(pageIndex),String.valueOf(pageSize),String.valueOf(allPage),String.valueOf(netBarInfoList.size()),totalSize!=null?totalSize.toString():0,netbarInfoRes.toString());
		return sRet;
	}
	public int removePcMac(String userId, String mac) {
		int iRet = -9999 ;
		if (StringUtils.hasText(userId) && StringUtils.hasText(mac)) {
			ComputerMac cMac = new ComputerMac();
			cMac.setMac(mac);
			cMac.setUseracct(userId);
			if (hotelComputerService.deleteHotelMac(cMac) == 1) {
				iRet = 0;
			} else {
				iRet = -2;// 不存在数据
			}

		} else {
			iRet = -1;// 参数不能为空
		}
		return iRet;
	}
	public int updatePcMac(String userId, String mac, String newMac) {
		int iRet = -9999 ;
		if (StringUtils.hasText(userId) && StringUtils.hasText(mac)&& StringUtils.hasText(newMac)  ) {
			if (hotelComputerService.updateHotelMac(userId, mac, newMac) == 1) {
				iRet = 0;
			} else {
				iRet = -2;// 不存在数据
			}

		} else {
			iRet = -1;// 参数不能为空
		}
		return iRet;
	}

	/**
	 * 保存酒店桌面模版页面的推送文件
	 * @param useracct：酒店号
	 * @param name：酒店名称
	 * @param namepy：酒店拼音
	 * @param updateSrvIds：更新节点，1是指的内部节点，3指的电信节点，4，指的是联通节点
	 * @param filePath：文件的路径
	 * @param fileSize：文件的大小
	 */
	public int saveHotelWebFile(String useracct,String name,String namepy,String updateSrvIds,String filePath,String fileSize){
		int iRet = -9999 ;
		try{
		    hotelComputerService.saveHotelWebFile(useracct, name, namepy, updateSrvIds, filePath, fileSize);
		
		    iRet = 0;//成功
		    
		}catch(Exception e){
			e.printStackTrace();
		}
		return iRet;
		
	}

}
